package com.laivi.basic.model.orm;

import com.laivi.basic.common.constants.Constant;
import com.laivi.basic.type.orm.CndType;

/**
 * @author laivi.zhu@gmail.com
 * @date 2014-7-8
 * description:
 */
public class Cnd {
	private StringBuffer where=new StringBuffer();
	
	private StringBuffer order;
	
	private StringBuffer group;
	
	public final static int PREFIX=1;
	
	public final static int ALL=2;
	
	public final static int SUFFIX=3;
	

	public static Cnd andCnd(String field,CndType cndType,Object value){
		return new Cnd().and(field, cndType, value);
	}
	
	public static Cnd andCnd(Cnd front,Cnd back){
		return new Cnd().and(front, back);
	}
	
	public static Cnd orCnd(Cnd front,Cnd back){
		return new Cnd().or(front, back);
	}
	
	public static Cnd likeCnd(String field,String value,int type){
		return new Cnd().like(field, value, type);
	}
	
	public static Cnd betweenCnd(String field,Object front,Object back){
		return new Cnd().between(field, front, back);
	}
	
	public static Cnd inCnd(String field,Object...objs){
		return new Cnd().in(field, objs);
	}
	
	public Cnd and(String field,CndType cndType,Object value){
		if(where.length()>0){
			where.append(" and ");
		}
		if(value instanceof String){
			where.append(field+cndType.toText()+"'"+value+"'");
		}else{
			where.append(field+cndType.toText()+value);
		}
		return this;
	}
	
	public Cnd like(String field,String value,int type){
		if(where.length()>0){
			where.append(" and ");
		}
		if(type==PREFIX){
			where.append(field+" like '%"+value+"'");
		}else if(type==ALL){
			where.append(field+" like '%"+value+"%'");
		}else if(type==SUFFIX){
			where.append(field+" like '"+value+"%'");
		}
		return this;
	}
	
	public Cnd and(Cnd front,Cnd back){
		if(where.length()>0){
			where.append(" and ");
		}
		where.append("(("+front.toCnd()+") and ("+back.toCnd()+"))");
		return this;
	}
	
	public Cnd or(Cnd front,Cnd back){
		if(where.length()>0){
			where.append(" and ");
		}
		where.append("(("+front.toCnd()+") or ("+back.toCnd()+"))");
		return this;
	}
	
	public Cnd between(String field,Object front,Object back){
		if(where.length()>0){
			where.append(" and ");
		}
		if(front instanceof String){
			where.append(field+" between '"+front+"' and '"+back+"'");
		}else{
			where.append(field+" between "+front+" and "+back);
		}
		return this;
	}
	
	public Cnd in(String field,Object...objs){
		if(where.length()>0){
			where.append(" and ");
		}
		if(objs.length>0){
			where.append(field+" in (");
			for(Object obj:objs){
				if(obj instanceof String){
					where.append("'"+obj+"',");
				}else{
					where.append(obj+",");
				}
			}
			where=new StringBuffer(where.substring(0, where.length()-1));
			where.append(")");
		}
		return this;
	}
	
	public Cnd asc(String field){
		return order(field,CndType.ASC);
	}
	
	public Cnd desc(String field){
		return order(field,CndType.DESC);
	}
	
	public Cnd order(String field,CndType type){
		if(order==null||order.length()==0){
			order=new StringBuffer();
			order.append(field+" "+type.toText());
		}else{
			order.append(","+field+" "+type.toText());
		}
		return this;
	}
	
	public Cnd group(String...fields){
		if(fields.length>0){
			group=new StringBuffer();
			for(String field:fields){
				group.append(field+",");
			}
			group=new StringBuffer(group.substring(0, group.length()-1));
		}
		return this;
	}
	
	public String toSql(){
		StringBuffer sql=new StringBuffer();
		if(where.length()!=0){
			sql.append(" where "+where);
		}
		if(group!=null && group.length()!=0){
			sql.append(" group by "+group.toString());
		}
		if(order!=null && order.length()!=0){
			sql.append(" order by "+order);
		}
		return sql.toString();
	}
	
	public String toWhere(){
		if(where.length()>0){
			return " where "+where.toString();
		}
		return Constant.EMPTY;
	}
	
	public String toOrder(){
		if(order.length()>0){
			return " order by "+order.toString();
		}
		return Constant.EMPTY;
	}
	
	public String toCnd(){
		return where.toString();
	}

}
